<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Daemonc_model extends MY_Model {

	function __construct() {
		parent::__construct();
		$this->tableName = "daemonc";
		$this->prefix = "dae";
	}

	function getDeamonc($date) {
		$qs =	"
					select
						dae_cnt
					from
						".$this->db->dbprefix.$this->tableName."
					where
						dae_date = '".$this->db->escape_str($date)."'
					";
		$result = $this->db->query($qs);
		foreach ($result->result() as $row) {
			return $row->dae_cnt;
		}
		return 0;
	}

	function runDaemonc() {
		//$this->output->enable_profiler(TRUE);
		//$this->load->model("institute");
		$query = "update ".$this->db->dbprefix."institute left join ".$this->db->dbprefix."city on city_id = ins_city_id and city_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = if(ins_ins_id is null, 'INS', 'FAC') and e2c_ent_id = ins_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set ins_presenter = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("accomodation");
		$query = "update ".$this->db->dbprefix."accomodation left join ".$this->db->dbprefix."acc2ins_maj on acc_id = a2im_acc_id and a2im_del = 0 left join ".$this->db->dbprefix."institute on ins_id = a2im_ins_id and ins_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'ACC' and e2c_ent_id = acc_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set acc_presenter = if(e2c_id is not null and ins_presenter = 'I', 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("major");
		$query = "update ".$this->db->dbprefix."major left join ".$this->db->dbprefix."institute on ins_id = maj_ins_id and ins_del = 0 and ins_presenter = 'I' left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'MAJ' and e2c_ent_id = maj_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set maj_presenter = if(e2c_id is not null and coalesce(maj_list_from_date, cast('1900-12-31' AS date)) <= curdate() and coalesce(maj_list_till_date, cast('9999-12-31' AS date)) >= curdate() and ins_presenter = 'I', 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("scholarship");
		$query = "update ".$this->db->dbprefix."scholarship left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'SCH' and e2c_ent_id = sch_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set sch_presenter = if(e2c_id is not null and coalesce(sch_list_date, cast('9999-12-31' AS date)) >= curdate(), 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("country");
		$query = "update ".$this->db->dbprefix."country left join ".$this->db->dbprefix."city on cou_id = city_cou_id and city_del = 0 left join ".$this->db->dbprefix."institute on city_id = ins_city_id and ins_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'COU' and e2c_ent_id = cou_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set cou_presenter = if(e2c_id is not null or ins_presenter = 'I', 'I', 'N'), cou_data = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("city");
		$query = "update ".$this->db->dbprefix."city left join ".$this->db->dbprefix."institute on city_id = ins_city_id and ins_del = 0 left join ".$this->db->dbprefix."country on cou_id = city_cou_id and cou_del = 0 left join ".$this->db->dbprefix."ent2cat on e2c_entity = 'CITY' and e2c_ent_id = city_id and e2c_del = 0 and coalesce(e2c_int, e2c_text) is not null set city_presenter = if(e2c_id is not null or ins_presenter = 'I', 'I', 'N'), city_data = if(e2c_id is not null, 'I', 'N')";
		$this->db->query($query);
		
		//$this->load->model("daemonc_model");
		$query = "insert into ".$this->db->dbprefix."daemonc (dae_date, dae_cnt) values(curdate(), 1) on duplicate key update dae_cnt = dae_cnt + 1";
		$this->db->query($query);
		
		$this->db->cache_delete_all();
	}

}